package hottargui.standard;

import java.util.Map;

import hottargui.framework.*;


public class StandardNewGameState implements TarguiState {
	
	Game gameContext;
	
	public StandardNewGameState(Game game)
	{
		this.gameContext = game;
	}
	
	public boolean buy(int count, Position deploy) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public State getState() {
		return State.newGame;
	}

	public boolean move(Position from, Position to, int count) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public void newGame() throws IllegalActionException {
		GameData gd = gameContext.getGameObjectServer().getGameData();
		
		// Setup board
		gameContext.getGameObjectServer().getBoardStrategy().setupBoard(gd);
		System.out.println("Get alpha Board");
		Map<PlayerColor, Player> players = gd.getPlayers();

		// setup players
		Player redPlayer = new StandardPlayer(PlayerColor.Red);
		((StandardPlayer)redPlayer).setSettlement(new Position(0, 0));
		players.put(PlayerColor.Red, redPlayer);
		
		Player greenPlayer = new StandardPlayer(PlayerColor.Green);
		((StandardPlayer)greenPlayer).setSettlement(new Position(0, 6));
		players.put(PlayerColor.Green, greenPlayer);
		
		Player bluePlayer = new StandardPlayer(PlayerColor.Blue);
		((StandardPlayer)bluePlayer).setSettlement(new Position(6, 0));
		players.put(PlayerColor.Blue, bluePlayer);
		
		Player yellowPlayer = new StandardPlayer(PlayerColor.Yellow);
		((StandardPlayer)yellowPlayer).setSettlement(new Position(6, 6));
		players.put(PlayerColor.Yellow, yellowPlayer);
		
		// setup rounds left (25 - the one about to take place)
		gd.setRoundsLeft(gameContext.getFactory().getGameRoundsStrategy().getRoundsToPlay() - 1);
	}

	public void rollDie() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public PlayerColor turnCard() throws IllegalActionException {
		GameData gd = gameContext.getGameObjectServer().getGameData();
		Map<PlayerColor, Player> players = gd.getPlayers();
		
		if ( players.isEmpty() )
		{
			// This makes any variant using StandardNewGameState cough
			// throw new IllegalActionException(gameContext.getState());
			
			// this is a valid alternative to IAE
			return PlayerColor.None;
		}
		else
		{
			gameContext.setState(gameContext.getFactory().getMoveState(gameContext));
			
			PlayerColor nextPlayer = gameContext.getGameObjectServer().getTurnSequenceStrategy().nextPlayer();
			return gameContext.getGameObjectServer().getGameData().setPlayerInTurn(nextPlayer);
		}
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}
	
	public void yieldAttack() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
	
	public void skip() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
}
